package com.cin.cma.server.pos.common.extension;


import com.cin.cma.common.enums.JyServerErrorCode;
import com.cin.cma.common.exception.UserFriendlyException;
import com.cin.cma.common.model.finixx.base.BaseResponse;
import org.springframework.stereotype.Component;

/**
 *
 * @Date：2024-07-18 11:14
 */
@Component
public class ErrorValid {
    /**
     * 验证票务上返回结果Result
     *
     * @param result 错误码
     * @throws UserFriendlyException 异常
     */
    public <T extends BaseResponse> void result(T result) throws UserFriendlyException {
        this.result(result, null);
    }

    /**
     * 验证票务上返回结果Result
     *
     * @param result 结果码
     * @param action 委托验证
     * @throws UserFriendlyException 异常
     */
    public <T extends BaseResponse> void result(T result, ActionExtension action) throws UserFriendlyException {
        switch (result.getResult()) {
            case 1004:
                throw new UserFriendlyException(JyServerErrorCode.FUNCTION_CODE_ERROR);
            case 1000:
                throw new UserFriendlyException(JyServerErrorCode.PASSPORT);
            case 1001:
                throw new UserFriendlyException(JyServerErrorCode.NEED_PERMISSION);
            case 1007:
                throw new UserFriendlyException(JyServerErrorCode.INVALID_SIGNATURE);
            case 1:
                throw new UserFriendlyException(JyServerErrorCode.FAILED);
            case 4:
                throw new UserFriendlyException(JyServerErrorCode.DATA_FORMAT_ERROR);
            case 2:
                throw new UserFriendlyException(JyServerErrorCode.EXCEPTION, result.getMessage());
            case 306:
            case 307:
                throw new UserFriendlyException(JyServerErrorCode.PASS_NUM_FALSE_THE_END);
            case 314:
                throw new UserFriendlyException(JyServerErrorCode.PASS_NUM_LENGTH_ERROR);
            case 308:
                throw new UserFriendlyException(JyServerErrorCode.PASS_NUM_CAN_NOT_BE_FOUND);
            case 305:
                throw new UserFriendlyException(JyServerErrorCode.PASS_NUM_FALSE_ON_GOING);
            case 6:
                throw new UserFriendlyException(JyServerErrorCode.PARAMS_EMPTY, result.getMessage());
            case 5:
                throw new UserFriendlyException(JyServerErrorCode.DATE_FORMAT_ERROR);
                //锁座失败，白名单，业务处理
            case 2011:
                break;
            default:
                if (action != null) {
                    action.invoke(result.getResult());
                }
                if (result.getResult() != 0) {
                    throw new UserFriendlyException(JyServerErrorCode.EXCEPTION, result.getMessage());
                }
                break;
        }
    }
}
